home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
- Begin VB.Form frmReflect
- Caption = "Reflect []"
- ClientHeight = 2895
- ClientLeft = 165
- ClientTop = 735
- ClientWidth = 3120
- LinkTopic = "Form2"
- ScaleHeight = 2895
- ScaleWidth = 3120
- StartUpPosition = 3 'Windows Default
- Begin VB.CommandButton cmdReflect
- Caption = "Reflect"
- Default = -1 'True
- Height = 375
- Left = 1920
- TabIndex = 2
- Top = 0
- Width = 855
- End
- Begin VB.TextBox txtB
- Height = 285
- Left = 1320
- TabIndex = 1
- Text = "50"
- Top = 0
- Width = 495
- End
- Begin VB.TextBox txtM
- Height = 285
- Left = 360
- TabIndex = 0
- Text = "0.5"
- Top = 0
- Width = 495
- End
- Begin VB.PictureBox picResult
- Height = 2295
- Left = 840
- ScaleHeight = 149
- ScaleMode = 3 'Pixel
- ScaleWidth = 157
- TabIndex = 4
- Top = 1320
- Visible = 0 'False
- Width = 2415
- End
- Begin MSComDlg.CommonDialog dlgOpenFile
- Left = 0
- Top = 360
- _ExtentX = 847
- _ExtentY = 847
- _Version = 393216
- End
- Begin VB.PictureBox picOriginal
- AutoSize = -1 'True
- Height = 2295
- Left = 120
- ScaleHeight = 149
- ScaleMode = 3 'Pixel
- ScaleWidth = 157
- TabIndex = 3
- Top = 480
- Width = 2415
- End
- Begin VB.Label Label1
- Caption = "B"
- Height = 255
- Index = 1
- Left = 1080
- TabIndex = 6
- Top = 0
- Width = 135
- End
- Begin VB.Label Label1
- Caption = "M"
- Height = 255
- Index = 0
- Left = 120
- TabIndex = 5
- Top = 0
- Width = 135
- End
- Begin VB.Menu mnuFile
- Caption = "&File"
- Begin VB.Menu mnuFileOpen
- Caption = "&Open..."
- Shortcut = ^O
- End
- Begin VB.Menu mnuFileSaveAs
- Caption = "Save &As..."
- Shortcut = ^A
- End
- End
- Attribute VB_Name = "frmReflect"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- Option Explicit
- Private M As Single
- Private B As Single
- Private sin_theta As Single
- Private cos_theta As Single
- ' Map the output pixel (ix_out, iy_out) to the input
- ' pixel (x_in, y_in).
- Private Sub MapPixel(ByVal ix_out As Single, ByVal iy_out As Single, ByRef x_in As Single, ByRef y_in As Single)
- Dim x1 As Single
- Dim y1 As Single
- Dim x2 As Single
- Dim y2 As Single
- Dim x3 As Single
- Dim y3 As Single
- Dim x4 As Single
- Dim y4 As Single
- ' Translate by (0, -B).
- x1 = ix_out
- y1 = iy_out - B
- ' Rotate by angle theta around the origin.
- x2 = x1 * cos_theta - y1 * sin_theta
- y2 = x1 * sin_theta + y1 * cos_theta
- ' Reflect.
- x3 = x2
- y3 = -y2
- ' Rotate by angle theta around the origin.
- x4 = x3 * cos_theta + y3 * sin_theta
- y4 = -x3 * sin_theta + y3 * cos_theta
- ' Translate by (0, +B).
- x_in = x4
- y_in = y4 + B
- End Sub
- ' Transform the image.
- Private Sub TransformImage(ByVal pic_from As PictureBox, ByVal pic_to As PictureBox)
- Dim white_pixel As RGBTriplet
- Dim input_pixels() As RGBTriplet
- Dim result_pixels() As RGBTriplet
- Dim bits_per_pixel As Integer
- Dim ix_max As Single
- Dim iy_max As Single
- Dim x_in As Single
- Dim y_in As Single
- Dim ix_out As Integer
- Dim iy_out As Integer
- Dim ix_in As Integer
- Dim iy_in As Integer
- Dim dx As Single
- Dim dy As Single
- Dim dx1 As Single
- Dim dx2 As Single
- Dim dy1 As Single
- Dim dy2 As Single
- Dim v11 As Integer
- Dim v12 As Integer
- Dim v21 As Integer
- Dim v22 As Integer
- ' Set the white pixel's value.
- With white_pixel
- .rgbRed = 255
- .rgbGreen = 255
- .rgbBlue = 255
- End With
- ' Get the pixels from pic_from.
- GetBitmapPixels pic_from, input_pixels, bits_per_pixel
- ' Get the pixels from pic_to.
- GetBitmapPixels pic_to, result_pixels, bits_per_pixel
- ' Get the original image's bounds.
- ix_max = pic_from.ScaleWidth - 2
- iy_max = pic_from.ScaleHeight - 2
- ' Calculate the output pixel values.
- For iy_out = 0 To pic_to.ScaleHeight - 1
- For ix_out = 0 To pic_to.ScaleWidth - 1
- ' Map the pixel value from
- ' (ix_out, iy_out) to (x_in, y_in).
- MapPixel ix_out, iy_out, x_in, y_in
- ' Interpolate to find the pixel's value.
- ' Find the nearest integral position.
- ix_in = Int(x_in)
- iy_in = Int(y_in)
- ' See if this is out of bounds.
- If (ix_in < 0) Or (ix_in > ix_max) Or _
- (iy_in < 0) Or (iy_in > iy_max) _
- Then
- ' The point is outside the image.
- ' Use white.
- result_pixels(ix_out, iy_out) = white_pixel
- Else
- ' The point lies within the image.
- ' Calculate its value.
- dx1 = x_in - ix_in
- dy1 = y_in - iy_in
- dx2 = 1# - dx1
- dy2 = 1# - dy1
- With result_pixels(ix_out, iy_out)
- ' Calculate the red value.
- v11 = input_pixels(ix_in, iy_in).rgbRed
- v12 = input_pixels(ix_in, iy_in + 1).rgbRed
- v21 = input_pixels(ix_in + 1, iy_in).rgbRed
- v22 = input_pixels(ix_in + 1, iy_in + 1).rgbRed
- .rgbRed = _
- v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
- v21 * dx1 * dy2 + v22 * dx1 * dy1
-
- ' Calculate the green value.
- v11 = input_pixels(ix_in, iy_in).rgbGreen
- v12 = input_pixels(ix_in, iy_in + 1).rgbGreen
- v21 = input_pixels(ix_in + 1, iy_in).rgbGreen
- v22 = input_pixels(ix_in + 1, iy_in + 1).rgbGreen
- .rgbGreen = _
- v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
- v21 * dx1 * dy2 + v22 * dx1 * dy1
- ' Calculate the blue value.
- v11 = input_pixels(ix_in, iy_in).rgbBlue
- v12 = input_pixels(ix_in, iy_in + 1).rgbBlue
- v21 = input_pixels(ix_in + 1, iy_in).rgbBlue
- v22 = input_pixels(ix_in + 1, iy_in + 1).rgbBlue
- .rgbBlue = _
- v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
- v21 * dx1 * dy2 + v22 * dx1 * dy1
- End With
- End If
- Next ix_out
- Next iy_out
- ' Set pic_to's pixels.
- SetBitmapPixels pic_to, bits_per_pixel, result_pixels
- pic_to.Picture = pic_to.Image
- End Sub
- ' Arrange the controls.
- Private Sub ArrangeControls()
- Dim wid As Single
- ' Position the result PictureBox.
- picResult.Move _
- picOriginal.Left + picOriginal.Width + 120, _
- picOriginal.Top, picOriginal.Width, picOriginal.Height
- picResult.Line (0, 0)-(picResult.ScaleWidth, picResult.ScaleHeight), _
- picResult.BackColor, BF
- picResult.Picture = picResult.Image
- picResult.Visible = True
- ' This makes the image resize itself to
- ' fit the picture.
- picResult.Picture = picResult.Image
- ' Make the form big enough.
- If cmdReflect.Left + cmdReflect.Width > picResult.Left + picResult.Width Then
- wid = cmdReflect.Left + cmdReflect.Width
- Else
- wid = picResult.Left + picResult.Width
- End If
- Move Left, Top, wid + 237, _
- picResult.Top + picResult.Height + 816
- DoEvents
- End Sub
- ' Reflect the image.
- Private Sub cmdReflect_Click()
- ' Do nothing if no picture is loaded.
- If picOriginal.Picture = 0 Then Exit Sub
- ' Get the slope and intercept.
- On Error GoTo BError
- B = CSng(txtB.Text)
- On Error GoTo MError
- M = CSng(txtM.Text)
- On Error GoTo 0
- ' Draw the line of reflection for reference.
- picOriginal.Cls
- picOriginal.Line (0, B)-(picOriginal.ScaleWidth, B + M * picOriginal.ScaleWidth)
- ' Calculate the sine and cosine of the angle.
- ' The minus sign reverses the angle.
- sin_theta = -M / Sqr(M * M + 1)
- cos_theta = 1 / Sqr(M * M + 1)
- ArrangeControls
- Screen.MousePointer = vbHourglass
- picResult.Line (0, 0)-(picResult.ScaleWidth, picResult.ScaleHeight), _
- picResult.BackColor, BF
- DoEvents
- ' Reflect the image.
- TransformImage picOriginal, picResult
- Screen.MousePointer = vbDefault
- Exit Sub
- BError:
- MsgBox "Invalid intercept value"
- txtB.SetFocus
- Exit Sub
- MError:
- MsgBox "Invalid slope value"
- txtM.SetFocus
- Exit Sub
- End Sub
- ' Start in the current directory.
- Private Sub Form_Load()
- picOriginal.AutoSize = True
- picOriginal.ScaleMode = vbPixels
- picOriginal.AutoRedraw = True
- picResult.ScaleMode = vbPixels
- picResult.AutoRedraw = True
- dlgOpenFile.CancelError = True
- dlgOpenFile.InitDir = App.Path
- dlgOpenFile.Filter = _
- "Bitmaps (*.bmp)|*.bmp|" & _
- "GIFs (*.gif)|*.gif|" & _
- "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
- "Icons (*.ico)|*.ico|" & _
- "Cursors (*.cur)|*.cur|" & _
- "Run-Length Encoded (*.rle)|*.rle|" & _
- "Metafiles (*.wmf)|*.wmf|" & _
- "Enhanced Metafiles (*.emf)|*.emf|" & _
- "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
- "All Files (*.*)|*.*"
- Width = cmdReflect.Left + cmdReflect.Width + 120 + Width - ScaleWidth
- Height = picOriginal.Top + picOriginal.Height + 120 + Height - ScaleHeight
- End Sub
- ' Load the indicated file.
- Private Sub mnuFileOpen_Click()
- Dim file_name As String
- ' Let the user select a file.
- On Error Resume Next
- dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
- dlgOpenFile.ShowOpen
- If Err.Number = cdlCancel Then
- Exit Sub
- ElseIf Err.Number <> 0 Then
- Beep
- MsgBox "Error selecting file.", , vbExclamation
- Exit Sub
- End If
- On Error GoTo 0
- Screen.MousePointer = vbHourglass
- DoEvents
- file_name = Trim$(dlgOpenFile.FileName)
- dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
- - Len(dlgOpenFile.FileTitle) - 1)
- Caption = "Reflect [" & dlgOpenFile.FileTitle & "]"
- ' Open the original file.
- On Error GoTo LoadError
- picOriginal.Picture = LoadPicture(file_name)
- On Error GoTo 0
- picOriginal.Picture = picOriginal.Image
- ' Hide picResult.
- picResult.Visible = False
- If cmdReflect.Left + cmdReflect.Width > picOriginal.Left + picOriginal.Width Then
- Width = cmdReflect.Left + cmdReflect.Width + 120 + Width - ScaleWidth
- Else
- Width = picOriginal.Left + picOriginal.Width + 120 + Width - ScaleWidth
- End If
- Height = picOriginal.Top + picOriginal.Height + 120 + Height - ScaleHeight
- Screen.MousePointer = vbDefault
- Exit Sub
- LoadError:
- Screen.MousePointer = vbDefault
- MsgBox "Error " & Format$(Err.Number) & _
- " opening file '" & file_name & "'" & vbCrLf & _
- Err.Description
- End Sub
- ' Save the transformed image.
- Private Sub mnuFileSaveAs_Click()
- Dim file_name As String
- ' Let the user select a file.
- On Error Resume Next
- dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
- dlgOpenFile.ShowSave
- If Err.Number = cdlCancel Then
- Exit Sub
- ElseIf Err.Number <> 0 Then
- Beep
- MsgBox "Error selecting file.", , vbExclamation
- Exit Sub
- End If
- On Error GoTo 0
- Screen.MousePointer = vbHourglass
- DoEvents
- file_name = Trim$(dlgOpenFile.FileName)
- dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
- - Len(dlgOpenFile.FileTitle) - 1)
- Caption = "Reflect [" & dlgOpenFile.FileTitle & "]"
- ' Save the transformed image into the file.
- On Error GoTo SaveError
- SavePicture picResult.Picture, file_name
- On Error GoTo 0
- Screen.MousePointer = vbDefault
- Exit Sub
- SaveError:
- Screen.MousePointer = vbDefault
- MsgBox "Error " & Format$(Err.Number) & _
- " saving file '" & file_name & "'" & vbCrLf & _
- Err.Description
- End Sub
-